﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;


//add references to:
//1.  System.Web
//2.  System.Runtime.Serialziation


namespace AzureConfigTask
{
    class AzureTask : SPJobDefinition
    {
        
        public static string JobName = "Azure Configuration Data Demo Job";
        public override string DisplayName
        {
            get
            {
                return JobName;
            }
        }


        public AzureTask()
            : base()
        {
        }

        public AzureTask(string jobName, SPService srvc, SPServer srv, SPJobLockType lt)
            : base(jobName, srvc, srv, lt)
        {
        }

        public AzureTask(SPWebApplication wa)
            : base(JobName, wa, null, SPJobLockType.Job)
        {
            //job lock configured to only run once instead of on every server (SPJobLockType.None)
        }

        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication wa = null;
            string errorMessage = string.Empty;
            
            try
            {
                wa = (SPWebApplication)this.Parent;

                //create an instance of the control
                AzureWcfPage.WcfDataControl cfgCtrl = new AzureWcfPage.WcfDataControl();

                //NOTE:  IF YOU SEE RED SQUIGGLY LINES UNDER THE PROPERTY NAMES AND 
                //THEY SHOW AN ERROR LIKE 'DOES NOT CONTAIN A DEFINITION...', THEN IT MEANS
                //YOU ADDED A REFERENCE ONLY TO THE INHERITED CONTROL ASSEMBLY; YOU ALSO 
                //**MUST** ADD A REFERENCE TO THE BASE CLASS ASSEMBLY - AZURECONNECT.DLL

                //set the properties to retrieve data
                cfgCtrl.WcfUrl = "https://azurewcf.vbtoys.com/Customers.svc";
                cfgCtrl.OutputType = AzureConnect.WcfConfig.DataOutputType.None;
                cfgCtrl.MethodName = "GetAllCustomers";

                //since there's no Http context in a task like a timer job, you also need to
                //provide the Url to a claims-enabled SharePoint site.  That address will be used
                //to connect to the STS endpoint for the SharePoint farm
                cfgCtrl.SharePointClaimsSiteUrl = wa.Sites[0].RootWeb.Url;

                //execute the method
                bool success = cfgCtrl.ExecuteRequest();

                //check for success
                if (success)
                {
                    //now retrieve the data and do whatever with it
                    AzureWcfPage.CustomersWCF.Customer[] Customers = 
                        (AzureWcfPage.CustomersWCF.Customer[])cfgCtrl.QueryResultsObject;

                    string AzureResults = cfgCtrl.QueryResultsString;

                    //this is where you would then do your tasks based on the data you got from Azure
                    foreach(AzureWcfPage.CustomersWCF.Customer c in Customers)
                    {
                        Debug.WriteLine(c.Email);
                    }

                    Debug.WriteLine(AzureResults);
                }
                else
                {
                    //do something to log the fact that data wasn't retrieved
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    }
}
